﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LastStand.gameContent.Friendlies;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;

namespace LastStand.gameContent.Friendlies
{
    public class Rifleman : Friendly
    {
        List<Sprite> enemyList;
        List<Shot> shotList;
        Texture2D shot;

        public Rifleman(Vector2 pos, Texture2D texture, List<Sprite> enemyList, List<Shot> shotList, Texture2D shot)
        {
            //ALL TO BE ADJUSTED
            this.rankName = rankNames[0];
            this.AttackDistance = 250;
            this.AttackPower = 5;
            this.attackInterval = 4000f;
            this.RankUpCost = 500;

            //prolly not needed
            this.Accuracy = 3;

            this.Position = pos;
            this.Texture = texture;
            this.enemyList = enemyList;
            this.shot = shot;
            this.shotList = shotList;
        }

		public override void Update(GameTimerEventArgs elapsedTime)
        {
            base.Update(elapsedTime);
            
            //increases the attacktimer...
            this.attackTimer += elapsedTime.ElapsedTime.Milliseconds;
            //until it exceeds the unites attack interval...
            if (this.attackTimer >= this.attackInterval)
            {
                //and allows the unit to attack
                this.canAttack = true;
            }
			Sprite focusEnemy = null;

            //when the unit is allowed to attack
            if (this.canAttack && enemyList.Count != 0)
            {
                //start with the first enemy, regardless of its position
                focusEnemy = enemyList[0];
                //implement a way of reading an enemy list, getting the closest enemies position
                //then having the unit attempt a shot at that enemy.
                foreach (Sprite enemy in enemyList)
                {
                    if (enemy.GetDistance(this.GetPosition) < focusEnemy.GetDistance(this.GetPosition)
                        && enemy.GetPosition.X > this.GetPosition.X)
                    {
                        focusEnemy = enemy;
                    }
                }
                //double check that this is indeed an enemy to prevent friendly fire.. until later *evil grin*
                if (focusEnemy != null && focusEnemy.GetPosition.X > this.GetPosition.X)
                {
					//find the angle of the shot now... include leading it a bit
					float angle;

					angle = (float)Math.Atan2(focusEnemy.GetPosition.Y - this.GetPosition.Y,
										focusEnemy.GetPosition.X  - this.GetPosition.X);

                    shotList.Add(new Shot(this.GetPosition, angle, new Vector2(20, 20), focusEnemy.GetPosition, shot));
                    this.canAttack = false;
                    this.attackTimer = 0;
                }
                
            }
            
            //Is the unit upgradable (via user input), 
            //then upgrade the unit to its next level
            //if (canRankUp)
               
        }

        public override void Draw(SpriteBatch sb)
        {
            base.Draw(sb);

            sb.Draw(this.Texture,
                    this.GetPosition,
                    null,
                    Microsoft.Xna.Framework.Color.White,
                    0.0f,
                    Vector2.Zero,
                    1.0f,
                    SpriteEffects.None,
                    0);
        }
    }
}
